package com.wlgc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wlgc.dao.UserDao;
import com.wlgc.entity.PageResult;
import com.wlgc.entity.User;
import com.wlgc.entity.dto.UserPageQueryDTO;
import com.wlgc.entity.vo.UserPageVO;
import com.wlgc.mapper.UserMapper;
import com.wlgc.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    UserDao dao;
    @Resource
    UserMapper userMapper;

    public User getUserByUid(int  uid)
    {

        return userMapper.getUserByUid(uid);

    }

    @Override
    public User getUserBySid(int sid) {

        return userMapper.getUserBySid(sid);
    }

    public  User getUserByUsername(String username)
    {
        LambdaQueryWrapper<User> wrapper;
        wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,username);
        return dao.selectOne(wrapper);
    }
    public User getUserByEmail(String email)
    {
        return userMapper.getUserByEmail(email);
    }
    @Override
    @Transactional
    public boolean addUser(String username, String password, int sid) {
       try{
           userMapper.addUser(username,password,"static/picture/user.jpg");
           userMapper.add_user_student(username,sid);
           userMapper.add_user_role(username,"admin");
       }
       catch (Exception e)
       {
           return  false;
       }
        return true;
    }

    @Override
    public int changeAvatar_Path(int uid, String avatar_path) {
      return   userMapper.changeAvatar_Path(uid,avatar_path);
    }

    @Override
    public String getAvatar_PathByUsername(String username) {
        return userMapper.getAvatar_PathByUsername(username);
    }

//    根据班级分页查询用户
    @Override
    public PageResult pageQuery(UserPageQueryDTO userPageQueryDTO) {
        PageHelper.startPage(userPageQueryDTO.getPage(),userPageQueryDTO.getPageSize());
        Page<UserPageVO> userPageVOS = userMapper.pageQuery(userPageQueryDTO);
        return new PageResult(userPageVOS.getTotal(),userPageVOS.getResult());
    }
}
